<?php
class saveRingtoneInfoAction extends sfActions {
	public function execute(){
		$retVal = new stdClass();
		$retVal->success = true;

		$this->adminId = $this->getUser()->getAttribute('adminId');
		$this->adminLevel = $this->getUser()->getAttribute('adminLevel');

		$id 			= $this->getRequestParameter("id");
		$name 			= $this->getRequestParameter("name");
		$description 	= makeSafe($this->getRequestParameter("description"));
		$remark 		= $this->getRequestParameter("remark");
		$visible 		= $this->getRequestParameter("visible", "off") == "on" ? 1 : 0;
		$tags			= makeSafe($this->getRequestParameter('tagNames'));
		$avatar			= $this->getRequestParameter('avatarValue', 'off') == "on" ? 1 : 0;

		$Ringtone = new Ringtone();
		if ($id == null)
		{
			$Ringtone->setCreatedDatetime("now");
			$taskName 			= "Tạo ringtone mới";
			$taskDescription 	= "Tạo ringtone: " . $name;
			exit();
		}
		else
		{
			$Ringtone->setNew(false);
			$Ringtone->setId($id);

			$taskName 			= "Sửa ringtone";
			$taskDescription 	= "Sửa ringtone: " . $name;

			$Ringtone = RingtonePeer::doSelectOne($Ringtone->buildCriteria());
			if (!$Ringtone)
			{
				exit();
			}
		}

		$Ringtone->setName($name);
		// Get new value
		$artist1Id = $this->getRequestParameter('artist1Id');
		$artist2Id = $this->getRequestParameter('artist2Id');
		$artist3Id = $this->getRequestParameter('artist3Id');
		$Ringtone->setArtist1Id($artist1Id);
		$Ringtone->setArtist2Id($artist2Id);
		$Ringtone->setArtist3Id($artist3Id);

		// Set new value for song
		if ($artist1Id)
		{
			$cr = new Criteria();
			$cr->add(ArtistPeer::ID, $artist1Id);
			$artist = ArtistPeer::doSelectOne($cr);
			if ($artist)
			{
				$artistName =  $artist->getName();
				$Ringtone->setArtist1Name($artistName);
			}

		}
		if ($artist2Id)
		{
			$cr = new Criteria();
			$cr->add(ArtistPeer::ID, $artist2Id);
			$artist = ArtistPeer::doSelectOne($cr);
			if ($artist)
			{
				$artistName =  $artist->getName();
				$Ringtone->setArtist2Name($artistName);
			}
		}
		if ($artist3Id)
		{
			$cr = new Criteria();
			$cr->add(ArtistPeer::ID, $artist3Id);
			$artist = ArtistPeer::doSelectOne($cr);
			if ($artist)
			{
				$artistName =  $artist->getName();
				$Ringtone->setArtist3Name($artistName);
			}
		}

		$Ringtone->setDescription($description);
		$Ringtone->setRemark($remark);
		$Ringtone->setEnabled($visible);
		$Ringtone->setTags($tags);
		if($avatar){
			$Ringtone->setAvatarPath(null);
		}
		// Checkin file
		$Ringtone->setAdminCheckoutId(null);

		// Save ringtone into table ringtone
		$Ringtone->save();

		// Delete ringtone_alert if existed
		$cr = new Criteria();
		$cr->add(RingtoneAlertPeer::RINGTONE_ID, $id);
		RingtoneAlertPeer::doDelete($cr);

		// Writte log
		$adminTaskMonitor = new AdminTaskMonitor();
		$adminTaskMonitor->setNew(true);
		$adminTaskMonitor->setAdminId($this->adminId);
		$adminTaskMonitor->setTaskName($taskName);
		$adminTaskMonitor->setTaskDescription($taskDescription);
		$adminTaskMonitor->setExcutedTime('now');
		$adminTaskMonitor->save();
		
		echo FastJSON::encode($retVal);
		exit();
	}
}
?>